<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/24
 * Time: 16:17
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
use Think\Think;

class FuturesAgentApplyController extends AdminController{
    /**
     * 经纪人列表
     * */
    public function index(){
    	//状态查询
    	if($_REQUEST['status']){
    		if($_REQUEST['status']==1){//已确认
    			$sql['a.status']=1;
    			$status=1;
    		}elseif($_REQUEST['status']==2){//已否定
    			$sql['a.status']=2;
    			$status=2;
    		}elseif($_REQUEST['status']==3){//未操作
    			$sql['a.status']=0;
    			$status=3;
    		}else{
    			$status=-1;
    		}
    	}
    	else{
    		$status=-1;
    	}
    	//下单者查询
    	if($_REQUEST['nickname']){
    		$map['nickname']=array('like',"%".trim($_REQUEST['nickname'])."%");
    		$user = M('Follow')->where($map)->field('id')->select();
    		foreach($user as $v){
    			$uid = $uid.$v['id'].',';
    		}
    		$uid = trim($uid,',');
    		$sql['user_id'] = array('in',$uid);
    	}
    	//电话号码查询
    	if($_REQUEST['mobile']){
    		$map['mobile'] = array('like',"%".trim($_REQUEST['mobile'])."%");
    		$user = M('Follow')->where($map)->field('id')->select();
    		foreach($user as $v){
    			$uid = $uid.$v['id'].',';
    		}
    		$uid = trim($uid,',');
    		$sql['user_id'] = array('in',$uid);
    	}
    	//申请时间查询
    	if ($_REQUEST['apply_time']) {
    		$time=strtotime($_REQUEST['apply_time']);
    		$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
    		$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
    		$sql['apply_time'] = array('between',"$start_time,$end_time");
    	}
    	$list = M('Futures_agent_apply')
    	->table('wp_futures_agent_apply as a')
    	->join('wp_follow as b on a.user_id = b.id')
    	->where($sql)
    	->field('a.id,b.nickname,b.mobile,a.apply_time,a.status')
    	->order('id desc')->select();
    	$this->assign('status',$status);
    	$this->assign('list',$list);
 		$this->display();
    }
    /**
     *  操作
     * */
    public function operation(){
    	$id = $_GET['id'];
    	//获取用户
    	$user_id = M('Futures_agent_apply')->where('id='.$id)->field('user_id')->find();
    	$user = M('Follow')->where('id='.$user_id['user_id'])->field('is_agent')->find();
    	//确定操作
    	if($_GET['operation'] == 1){
    		//判断是否已经是经纪人
    		if($user['is_agent'] == 1){
    			M('Futures_agent_apply')->where('id='.$id)->setField('status','1');
    			$this->error('该用户已经是经纪人');
    		}else{
    			M('Follow')->where('id='.$user_id['user_id'])->setField('is_agent','1');
    			M('Futures_agent_apply')->where('id='.$id)->setField('status','1');
    			$this->success('已确认');
    		}
    	}
    	//否认操作
    	if($_GET['operation'] == 2){
    		//判断是否已经是经纪人
    		if($user['is_agent'] == 1){
    			$this->error('该用户已经是经纪人');
    		}else{
    			M('Futures_agent_apply')->where('id='.$id)->setField('status','2');
    			$this->success('已否定');
    		}
    	}
    }
}